package org.example.c2;

import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

/**
 * @author liu-rui
 * @date 2020/6/1 上午11:33
 * @description 对比Random和ThreadLocalRandom性能
 * @since
 */
public class Demo5 {
    public static void main(String[] args) throws RunnerException {
        Options opt = new OptionsBuilder()
                // 导入要测试的类
                .include(Demo5.class.getName())
                // 预热5轮
                .warmupIterations(5)
                // 度量10轮
                .measurementIterations(10)
                .mode(Mode.Throughput)
                .forks(3)
                .build();

        new Runner(opt).run();
    }

    /**
     * 测试Random
     */
    @Benchmark
    public void testRandom() {
        new Random().nextInt();
    }

    static Random random = new Random();
    /**
     * 测试Random，ｒａｎｄｏｍ是单例的
     */
    @Benchmark
    public void testSingtonRandom() {
        random.nextInt();
    }

    /**
     * 测试ThreadLocalRandom
     */
    @Benchmark
    public void testThreadLocalRandom() {
        ThreadLocalRandom.current().nextInt();
    }
}
